package me.sdevil507.biz.fruit.controller;

import com.alibaba.druid.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import me.sdevil507.biz.fruit.dto.FruitDTO;
import me.sdevil507.biz.fruit.dto.FruitOrgRelDTO;
import me.sdevil507.biz.fruit.dto.FruitParamsDTO;
import me.sdevil507.biz.fruit.dto.ResultListFruitDTO;
import me.sdevil507.biz.fruit.mapper.FruitMapper;
import me.sdevil507.biz.fruit.po.FruitFeedBackPO;
import me.sdevil507.biz.fruit.po.FruitOrgRelPo;
import me.sdevil507.biz.fruit.po.FruitPO;
import me.sdevil507.biz.fruit.po.ResultPO;
import me.sdevil507.biz.fruit.repository.FruitRepository;
import me.sdevil507.biz.fruit.service.FruitImportExportService;
import me.sdevil507.biz.fruit.service.FruitOrgRelService;
import me.sdevil507.biz.fruit.service.FruitService;
import me.sdevil507.org.service.TempExpertService;
import me.sdevil507.supports.result.ApiResultDTO;
import me.sdevil507.supports.result.ApiResultGenerator;
import me.sdevil507.supports.shiro.helper.OrgAccountHelper;
import me.sdevil507.supports.status.ApiStatusCode;
import me.sdevil507.supports.status.ComConstant;
import me.sdevil507.systemAssist.Constant.SystemAssistConstant;
import me.sdevil507.systemAssist.systemLog.annotation.OperLog;
import me.sdevil507.systemAssist.systemLog.service.SystemErrorLogService;
import me.sdevil507.systemAssist.systemLog.service.SystemOperLogService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @program: tips_back_end
 * @description: 成果展示类
 **/
@RestController
@RequestMapping("/api/fruit")
@Slf4j
public class FruitController {

    @Autowired
    private FruitService fruitService;

    @Autowired
    private FruitOrgRelService fruitOrgRelService;

    @Autowired
    private FruitImportExportService fruitImportExportService;

    @Autowired
    private FruitMapper fruitMapper;

    @Autowired
    private TempExpertService tempExpertService;

    @Resource
    private FruitRepository fruitRepository;

    @Autowired
    private SystemOperLogService systemOperLogService;
    @Autowired
    private SystemErrorLogService systemErrorLogService;


    /**
     * 成果列表查询
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/readAllFruitByPage")
    @RequiresPermissions(value={"org:fruit:view","expert:fruit:view"}, logical= Logical.OR)
    public ApiResultDTO readAllFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<ResultListFruitDTO> pagePos = fruitService.readAllFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    /**
     * 专报内参 分页查询
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/readAllOrgSpecialFruitByPage")
    public ApiResultDTO readAllOrgSpecialFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<ResultListFruitDTO> pagePos = fruitService.readAllOrgSpecialFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 主页搜索
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/readAllHomeFruitByPage")
    public ApiResultDTO readAllHomeFruitByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<ResultListFruitDTO> pagePos = fruitService.readAllHomeFruitByPage(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 主页高级搜索
     * @param fruitParamsDTO
     * @return
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_SEARCH, operDesc = "")
    @PostMapping("/readAllHomeFruitOfSearchByPage")
    public ApiResultDTO readAllHomeFruitOfSearchByPage(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<ResultListFruitDTO> pagePos = fruitService.readAllHomeFruitOfSearchByPage(fruitParamsDTO);

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 查询全部记录
     *
     * @param fruitParamsDTO 搜索DTO
     * @return 记录列表
     */
    @PostMapping("/readAll")
    public ApiResultDTO getAll(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            List<ResultListFruitDTO> list = fruitService.readAll(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getSortDto());
            apiResultDto = ApiResultGenerator.create("0", "获取信息列表成功!", list);
        } catch (Exception e) {
            log.error("获取信息列表失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "获取信息列表失败!");
        }
        return apiResultDto;
    }

    /**
     * 获取单个成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/getFruitById")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitById(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            FruitPO fruitPo = fruitService.findOneById(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitPo);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 成果名称是否已经存在---机构端查询重复
     *
     * @return 执行反馈
     */
    @PostMapping("/findIsExistFruitName")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO findIsExistFruit(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        fruitService.setBusinessId(dto);

        FruitPO fruitPO = fruitService.findIsExistFruit(dto);
        if (fruitPO != null) {
            String msg = "";
            if(!StringUtils.isEmpty(fruitPO.getActivityProdType())){
                if(ComConstant.FruitType.PolicyAdvice.equals(fruitPO.getType())){
                    if(ComConstant.FruitType.MEETING.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【活动成果】的出席会议类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.RECEIVE_VISITORS.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【活动成果】的接待来访类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.SURVEY.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【活动成果】的考察调研类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.TRAINING.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【活动成果】的授课培训类别中已有相同名称的成果";
                    }
                }else if(ComConstant.FruitType.PROJECT.equals(fruitPO.getType())){
                    if(ComConstant.FruitType.VERTICAL.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【课题/项目信息】的纵向项目类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.HORIZONTAL.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【课题/项目信息】的横向项目类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.PROVINCEFOUCS.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【课题/项目信息】的省重点智库课题类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.HIGHFOUCS.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【课题/项目信息】的国家高端智库年度重点课题类别中已有相同名称的成果";
                    }else if(ComConstant.FruitType.SELFSET.equals(fruitPO.getActivityProdType())){
                        msg = "检测到【课题/项目信息】的自设课题类别中已有相同名称的成果";
                    }
                }
            }else{
                msg = "检测到当前类别已有相同名称的成果";
            }

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.FRUIT_NAME_EXISTED.getCode(), msg);
            return apiResultDTO;
        }
        apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), null);

        return apiResultDTO;
    }

    /**
     * 专报内参 查询是否有新反馈
     * @param fruitParamsDTO
     * @return
     */
    @PostMapping("/readAllOrgSpecialFruitFeedBackCount")
    public ApiResultDTO readAllOrgSpecialFruitFeedBackCount(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            int allFeedBackCount = fruitService.readAllOrgSpecialFruitFeedBackCount(fruitParamsDTO.getSearchDto());

            apiResultDto = ApiResultGenerator.create("0", "专报内参 查询是否有新反馈获取成功!", allFeedBackCount);
        } catch (Exception e) {
            log.error("专报内参 查询是否有新反馈获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "专报内参 查询是否有新反馈获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 专报反馈列表
     *
     * @return 执行反馈
     */
    @PostMapping("/getFeedBackListByFruitId")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFeedBackListByFruitId(@RequestBody FruitFeedBackPO fruitFeedBackPO) {
        ApiResultDTO apiResultDTO;
        try {
            List<FruitFeedBackPO> fruitFeedBackPOS = fruitService.getOneFruitFeedBackByFruitId(fruitFeedBackPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), fruitFeedBackPOS);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取专报反馈列表失败");
        }
        return apiResultDTO;
    }

    /**
     * 更新成果的反馈信息
     *
     * @return 执行反馈
     */
    @PostMapping("/updateFruitFeedBackByFruitId")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO updateFruitFeedBackByFruitId(@RequestBody FruitFeedBackPO fruitFeedBackPO) {
        ApiResultDTO apiResultDTO;
        try {
            fruitService.updateFruitFeedBackByFruitId(fruitFeedBackPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            log.error("更新失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "更新失败");
        }
        return apiResultDTO;
    }



    @PostMapping("/searchFruitNotInProjectData")
    public ApiResultDTO searchFruitNotInProjectData(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDto;
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            Page<FruitPO> pagePos = fruitService.searchFruitNotInProjectData(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getPageDto(), fruitParamsDTO.getSortDto());

            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", pagePos);
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }

    /**
     * 查询是否报送过 智库办
     * @param fruitDTO
     * @return
     */
    @PostMapping("/isHaveBsJSSWXCB")
    public ApiResultDTO isHaveBsJSSWXCB(@RequestBody FruitDTO fruitDTO) {
        ApiResultDTO apiResultDto;
        try {
            FruitPO fruitPO = fruitService.findOneById(fruitDTO.getId());
            apiResultDto = ApiResultGenerator.create("0", "分页数据获取成功!", fruitService.isHaveBsJSSWXCB(fruitPO.getSubList()));
        } catch (Exception e) {
            log.error("分页数据获取失败:", e);
            apiResultDto = ApiResultGenerator.create("-1", "分页数据获取失败!");
        }
        return apiResultDto;
    }


    /**
     * 报送到智库专报
     *
     * @param fruitDTO 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_ADD, operDesc = "")
    @PostMapping("/subNcToSpecial")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO subNcToSpecial(@RequestBody FruitDTO fruitDTO) {
        ApiResultDTO apiResultDTO;
        // 执行新增
        try {
            fruitService.subNcToSpecial(fruitDTO);
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("报送到智库专报失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "报送到智库专报失败");
        }
        return apiResultDTO;
    }

    /**
     * 机构新增成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_ADD, operDesc = "")
    @PostMapping("/create")
    @RequiresPermissions(value={"org:fruit:add","expert:fruit:add"}, logical= Logical.OR)
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO register(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            //TODO  专家没有权限概念（标识有限制） 机构端和专家端 需拆开此方法
            if(ComConstant.ModuleTerminal.ORGANIZATION == dto.getTerminal()){
                fruitService.orgCreate(dto);
            }else if(ComConstant.ModuleTerminal.EXPERT == dto.getTerminal()){
                fruitService.expertCreate(dto);
            }
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("添加成果失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "新增成果失败");
        }
        return apiResultDTO;
    }


    /**
     * 修改
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_UPDATE, operDesc = "")
    @PostMapping("/update")
    @RequiresPermissions(value={"org:fruit:edit","expert:fruit:edit"}, logical= Logical.OR)
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO update(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        try {
            if(ComConstant.ModuleTerminal.ORGANIZATION == dto.getTerminal()){
                fruitService.orgCreate(dto);
            }else if(ComConstant.ModuleTerminal.EXPERT == dto.getTerminal()){
                fruitService.expertCreate(dto);
            }
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("更新成果失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "更新成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 删除成果
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_DELETE, operDesc = "")
    @PostMapping("/delete")
    @RequiresPermissions(value={"org:fruit:delete","expert:fruit:delete"}, logical= Logical.OR)
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO deleteFruit(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            if(ComConstant.ModuleTerminal.ORGANIZATION == dto.getTerminal()){
                Long orgId = OrgAccountHelper.getOrgId();
                if(orgId == null){
                    return ApiResultGenerator.create("6001", "当前用户非机构管理员，无权限删除！");
                }else{
                    fruitService.orgDelete(dto.getId());
                }
            }else if(ComConstant.ModuleTerminal.EXPERT == dto.getTerminal()){
                fruitService.expertDelete(dto.getId());
            }
            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription());
        } catch (Exception e) {
            //事务回滚
            log.error("删除成果失败", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            apiResultDTO = ApiResultGenerator.create("6001", "删除成果失败");
        }
        return apiResultDTO;
    }

    /**
     * 获取导出可选字段
     *
     * @return 执行反馈
     */
    @PostMapping("/getExcelFields")
    public ApiResultDTO getExcelFields(@RequestBody FruitDTO searchDto) {
        ApiResultDTO apiResultDTO;
        List<Map<String, String>> mapLists =  fruitImportExportService.getExcelFields(searchDto);

        apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), mapLists);
        return apiResultDTO;
    }

    /**
     * 导出成果
     *
     * @param fruitParamsDTO 注册入参dto
     * @return 执行反馈
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_EXPORT, operDesc = "", recordRequestParams = false)
    @PostMapping("/export")
    @RequiresPermissions(value={"org:fruit:export","expert:fruit:export"}, logical= Logical.OR)
    @Transactional(rollbackFor = Exception.class)
    public void exportFruit(@RequestBody FruitParamsDTO fruitParamsDTO, HttpServletResponse response) throws IOException {

        fruitService.setBusinessId(fruitParamsDTO);
        if (!StringUtils.isEmpty(fruitParamsDTO.getData())) {
            //获取前台选择导出字段集合,通过逗号做分隔符
            String data = fruitParamsDTO.getData();
            String[] splitData = data.split(",");
            String exportType = fruitParamsDTO.getExportType();

            //获取所有需要导出数据
            List<FruitPO> list = fruitService.readAllOrgFruit(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getSortDto());
            //导出excel
            fruitImportExportService.exportFruit(fruitParamsDTO, splitData, list, exportType, response);

        }

    }

    /**
     * @ClassName FruitOrgController
     * @Description : 导入成果数据
     *
     * @param file   excel文件
     * @param type   成果类型
     * @Return : me.sdevil507.supports.result.ApiResultDTO
     * @Author : kzl
     * @Date : 2020/8/14 16:11
     */
    @OperLog(operTerminal = SystemAssistConstant.systemAssistTerminal.ORG, operClassify = SystemAssistConstant.systemAssistClassify.FRUIT, operModule = SystemAssistConstant.systemAssistModule.FRUIT_IMPORT, operDesc = "", recordRequestParams = false)
    @RequestMapping(value = "/importProject", method = RequestMethod.POST)
    @ResponseBody
    @RequiresPermissions(value={"org:fruit:import","expert:fruit:import"}, logical= Logical.OR)
    public ApiResultDTO importProject(@RequestParam("file") MultipartFile file, String type, String activityProdType) {
        try {
            // 获取文件原始名称
            String originalFileName = file.getOriginalFilename();
            // 创建临时文件
            assert originalFileName != null;
            File tempFile = File.createTempFile("temp", originalFileName.substring(originalFileName.lastIndexOf(".")));
            // 将上传文件转为临时文件
            file.transferTo(tempFile);
            // 退出时删除临时文件
            tempFile.deleteOnExit();
            // 执行导入
            return fruitImportExportService.importFruitByExcel(tempFile, type, activityProdType);
        } catch (IOException e) {
            log.error("导入成果数据失败:", e);
            return ApiResultGenerator.create("-1", "导入成果数据失败!");
        }
    }

    /**
     * 根据作者id 查询是否有对应的成果(is_del = 0)
     *
     * @param dto 注册入参dto
     * @return 执行反馈
     */
    @PostMapping("/getFruitByAuthorId")
    @Transactional(rollbackFor = Exception.class)
    public ApiResultDTO getFruitByAuthorId(@RequestBody FruitDTO dto) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            Boolean flag = fruitService.getFruitByAuthorId(dto.getId());

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), flag);
        } catch (Exception e) {
            log.error(" 获取成果失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "获取成果失败");
        }
        return apiResultDTO;
    }




    /**
     * @Description : 功能说明  获取不同级别的课题

     */
    @PostMapping("/levelCount")
    public ApiResultDTO countProjectByLevel(@RequestBody FruitParamsDTO fruitParamsDTO) {
        ApiResultDTO apiResultDTO;
        // 执行逻辑删除
        try {
            fruitService.setBusinessId(fruitParamsDTO);
            List<FruitPO> fruitList = fruitService.readAllOrgFruit(fruitParamsDTO.getSearchDto(), fruitParamsDTO.getSortDto());
            List<ResultPO> resultPOList = new ArrayList<>();
            ResultPO resultPO = new ResultPO();
            //国家级
            resultPO.setResultName("国家级");
            resultPO.setNum(fruitList.stream().filter(fruitPO -> ComConstant.FruitType.PROJECT.equals(fruitPO.getType()) &&
                    ("1").equals(fruitPO.getClientLevel())).count() + "");
            resultPOList.add(resultPO);
            //部级
            resultPO = new ResultPO();
            resultPO.setResultName("部级");
            resultPO.setNum(fruitList.stream().filter(fruitPO -> ComConstant.FruitType.PROJECT.equals(fruitPO.getType()) &&
                    ("2").equals(fruitPO.getClientLevel())).count() + "");
            resultPOList.add(resultPO);
            //省级
            resultPO = new ResultPO();
            resultPO.setResultName("省级");
            resultPO.setNum(fruitList.stream().filter(fruitPO -> ComConstant.FruitType.PROJECT.equals(fruitPO.getType()) &&
                    ("3").equals(fruitPO.getClientLevel())).count() + "");
            resultPOList.add(resultPO);
            //厅局级
            resultPO = new ResultPO();
            resultPO.setResultName("厅局级");
            resultPO.setNum(fruitList.stream().filter(fruitPO -> ComConstant.FruitType.PROJECT.equals(fruitPO.getType()) &&
                    ("4").equals(fruitPO.getClientLevel())).count() + "");
            resultPOList.add(resultPO);

            apiResultDTO = ApiResultGenerator.create(ApiStatusCode.SUCCESS.getCode(), ApiStatusCode.SUCCESS.getDescription(), resultPOList);
        } catch (Exception e) {
            //事务回滚
            log.error("统计成果数量失败", e);
            apiResultDTO = ApiResultGenerator.create("6001", "统计成果数量失败");
        }
        return apiResultDTO;
    }

}
